<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            width: 200px;
            height: 20px;
            line-height: 20px;
            background: pink;
        }
        
        li a {
            float: right;
        }
    </style>
</head>

<body>
    <div>
        <textarea name="" id=""></textarea>
        <button>发布</button>
        <ul>
        </ul>
    </div>
    <script>
        var btn = document.querySelector('button');
        var text = document.querySelector("textarea");
        var ul = document.querySelector("ul");
        btn.onclick = function() {
            //1创建元素

            if (text.value == '') {
                alert('您未输入任何内容！');
                return false;
            } else {
                var li = document.createElement("li");
                //将文本框的内容给添加的li
                li.innerHTML = text.value + "<a href='javascript:;'>删除</a>";
                //2.添加元素
                //在后面添加
                //ul.appendChild(li);
                //在前面添加
                ul.insertBefore(li, ul.children[0]);
                //  删除当前链接的li
                var as = document.querySelectorAll("a");
                for (var i = 0; i < as.length; i++) {
                    as[i].onclick = function() {
                        //node.removeChild(child)
                        ul.removeChild(this.parentNode);
                    }
                }
            }
        }
    </script>
</body>

</html>